package com.efast.cafe.portal.controller.api;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.efast.cafe.framework.bean.ResultBean;
import com.efast.cafe.framework.controller.AbstractController;
import com.efast.cafe.framework.exception.ServiceException;
import com.efast.cafe.framework.vo.DataTableVo;
import com.efast.cafe.portal.bean.api.ApiSystemDefineBean;
import com.efast.cafe.portal.bean.api.SystemQueryBean;
import com.efast.cafe.portal.entity.api.ApiSystemDefine;
import com.efast.cafe.portal.service.api.IApiSystemDefineService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContext;

import net.sf.json.JSONObject;

@Controller
@RequestMapping("/frame_center/api")
public class ApiSystemDefineController extends AbstractController {

	@Autowired
	private IApiSystemDefineService apiSystemDefineService;

	

	/**
	 * 页面跳转
	 * 
	 * @param searchValue
	 * @param model
	 * @param request
	 * @param response
	 * @return
	 */
//	@RequestMapping(value = "/system/page")
	public String templatePage(@RequestParam(required = false) String searchValue, ModelMap model,
			HttpServletRequest request, HttpServletResponse response) {
		return "/api/system/list";

	}

	/**
	 * 根据id查询系统详细信息,新增,查询，修改跳转到此页面
	 * 
	 * @param systemId
	 * @param modleAndView
	 * @param request
	 * @param response
	 * @return
	 */
//	@RequestMapping(value = "/system/detail")
	public ModelAndView SystemDetailPage(@RequestParam(required = false, value = "id") Integer systemId,
			HttpServletRequest request, HttpServletResponse response) {
		ModelAndView modleAndView = new ModelAndView("api/system/detail");
		ApiSystemDefine systemDetailsBean = null;
		if (systemId != null && systemId != 0) {
			systemDetailsBean = apiSystemDefineService.querySystemDetailsById(systemId);
		} else {
			systemDetailsBean = new ApiSystemDefineBean();
		}
		JSONObject json = JSONObject.fromObject(systemDetailsBean);
		modleAndView.addObject("systemDetailsBean", json);
		return modleAndView;
	}

	/**
	 * 以Table形式返回结果集
	 * 
	 * @param queryBean
	 * @return
	 */
//	@RequestMapping("/system/all")
	public @ResponseBody List<ApiSystemDefineBean> querySystemDefindList(SystemQueryBean queryBean) {
		return apiSystemDefineService.querySystemDefindList(queryBean);
	}

	/**
	 * 以Table形式返回结果集
	 * 
	 * @param queryBean
	 * @return
	 */
//	@RequestMapping("/system/list")
	public @ResponseBody DataTableVo<ApiSystemDefineBean> querySystemDefindPageList(SystemQueryBean queryBean) {
		return outPageJSONArray_bootstrapDatatable(apiSystemDefineService.querySystemDefindPageList(queryBean));
	}

	/**
	 * 根据id查询对应系统的详细信息， 以Table形式返回结果集
	 * 
	 * @param systemId
	 * @param queryBean
	 * @return
	 */
//	@RequestMapping("/system/queryById")
	public @ResponseBody ApiSystemDefineBean querySystemDetailsById(
			@RequestParam(required = true, value = "id") Integer systemId) {
		if (systemId != null && systemId != 0) {
			// return apiSystemDefineService.querySystemDetailsById(systemId);
		}
		return null;
	}

	/**
	 * 新增一个系统
	 * 
	 * @param request
	 * @param response
	 * @param systemName
	 * @param systemPath
	 * @param apiSystemDefine
	 * @return
	 */
//	@RequestMapping("/system/save")
	public @ResponseBody ResultBean saveSystemDefind(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = true) String systemName, @RequestParam(required = true) String systemPath,
			ApiSystemDefine apiSystemDefine) {
		RequestContext requestContext = new RequestContext(request);
		ResultBean resultBean = new ResultBean();
		try {
			checkSystemInfo(apiSystemDefine);
			// 参数有效性验证合格
			apiSystemDefineService.addNewSystemDefind(apiSystemDefine);
			resultBean.setSuccess(true);
			resultBean.setMessage(requestContext.getMessage(ADD_SUCCESS_TIP));
		} catch (Exception e) {
			resultBean.setSuccess(false);
			if (e instanceof ServiceException) {
				resultBean.setMessage(requestContext.getMessage(e.getMessage()));
			} else {
				resultBean.setMessage(requestContext.getMessage(ADD_FAILURE_TIP));
			}
			return resultBean;
		}
		return resultBean;

	}

	/**
	 * 根据id修改系统
	 * 
	 * @param request
	 * @param response
	 * @param systemId
	 * @param apiSystemDefine
	 * @return
	 */
//	@RequestMapping("/system/update")
	public @ResponseBody ResultBean updateSystemDefind(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(required = true, value = "id") Integer id, ApiSystemDefine apiSystemDefine,
			ApiSystemDefineBean systemInfoBean) {
		RequestContext requestContext = new RequestContext(request);
		ResultBean resultBean = new ResultBean(); // 返回结果bean
		try {
			checkSystemInfo(apiSystemDefine);
			// 参数有效性验证合格
			apiSystemDefineService.updateSystemDefind(id, apiSystemDefine, systemInfoBean);
			resultBean.setSuccess(true);
			resultBean.setMessage(requestContext.getMessage(EDIT_SUCCESS_TIP));
		} catch (Exception e) {
			resultBean.setSuccess(false);
			if (e instanceof ServiceException) {
				resultBean.setMessage(requestContext.getMessage(e.getMessage()));
			} else {
				resultBean.setMessage(requestContext.getMessage(EDIT_FAILURE_TIP));
			}
			return resultBean;
		}
		return resultBean;
	}

	/**
	 * 对apiSystemDefine参数有效应的验证
	 * 
	 * @Date 2017年4月21日
	 * @param apiSystemDefine
	 * @return
	 * @throws ServiceException
	 */
	private void checkSystemInfo(ApiSystemDefine apiSystemDefine) {
		String systemName = apiSystemDefine.getSystemName();
		String systemPath = apiSystemDefine.getSystemPath();
		if (!(systemName != null && !"".equals(systemName.trim()))) {
			throw new ServiceException("msg.portal.api.system.name.empty");
		}
		if (!(systemPath != null && !"".equals(systemPath.trim()))) {
			throw new ServiceException("msg.portal.api.system.uri.empty");
		}
		if (!systemPath.startsWith("/")) {
			throw new ServiceException("msg.portal.api.system.uri.error");
		}
	}

	/**
	 * 对apiSystemDefine参数有效应的验证
	 * 
	 * @Date 2017年4月21日
	 * @author :whxu
	 * @param systemInfoBean
	 * @return void
	 * @throws ServiceException
	 */
	private void checkSystemInfoBean(ApiSystemDefineBean systemInfoBean, RequestContext requestContext) {
		Integer systemStatus = systemInfoBean.getSystemStatus();
		String systemVsersion = systemInfoBean.getSystemVersion();
		String indentifer = systemInfoBean.getIndentifier();
		if (systemStatus == null) {
			throw new ServiceException("msg.portal.api.system.status.empty");
		}
		if (!(systemVsersion != null && !"".equals(systemVsersion.trim()))) {
			throw new ServiceException("msg.portal.api.system.version.empty");
		}
	}

	/**
	 * 排除已有该模块的系统
	 * 
	 * @Date : 2017年2月10日
	 * @param moduleUri
	 * @param moduleVersion
	 * @return
	 */
//	@RequestMapping("/system/findSystemIsNotId")
	public @ResponseBody List<ApiSystemDefineBean> findSystemIsNotId(@RequestParam(required = true) Integer moduleId) {
		List<ApiSystemDefineBean> liSystemDetailsBeans = apiSystemDefineService.findSystemIsNotId(moduleId);
		return liSystemDetailsBeans;
	}

	
	/**
	 * 系统复用
	 * @param apiSystemDefineBean
	 * @param systemId
	 * @param request
	 * @param response
	 * @return
	 */
//	@RequestMapping("/system/reuses")
	public @ResponseBody ResultBean reuseSystem(@ModelAttribute ApiSystemDefine apiSystemDefine,
			@RequestParam(required = true) Integer systemId, HttpServletRequest request, HttpServletResponse response) {
		RequestContext requestContext = new RequestContext(request);
		ResultBean resultBean = new ResultBean(); // 返回结果bean
		try {
			apiSystemDefineService.reusesSystem(systemId,apiSystemDefine);
			resultBean.setSuccess(true);
			resultBean.setMessage(requestContext.getMessage(""));
		} catch (Exception e) {
			resultBean.setSuccess(false);
			if (e instanceof ServiceException) {
				resultBean.setMessage(requestContext.getMessage(e.getMessage()));
			} else {
				resultBean.setMessage(requestContext.getMessage(""));
			}
		}
		return resultBean;
	}

}
